import axios from 'axios';
import { Toast } from 'vant';

const baseURL = process.env.NODE_ENV === 'production' ? '' : '/api'
const service = axios.create({
    baseURL: baseURL,
    // timeout: 6000
})


//请求拦截
service.interceptors.request.use(config => {
  
    // console.log(encodeURI(localStorage.getItem('accessToken')).replace('%20','+'));
    config.headers['Authorization']='token '+encodeURI(localStorage.getItem('accessToken')).replace('%20','+');
    //g+5Qu27JnNryZI0QoJLIw68ArQ1i22Y1O/fOgmaqnXY=
    // config.headers['Authorization'] = 'token g+5Qu27JnNryZI0QoJLIw68ArQ1i22Y1O/fOgmaqnXY='
    return config;
}, error => {
    Toast('网络错误，请稍后在试！')
    return Promise.resolve([true, error]);
});


//响应拦截
service.interceptors.response.use(response => {
    Toast.clear()
    return [null, response.data];
}, error => {
    Toast('网络错误，请稍后在试！');
    return Promise.resolve([true, error.response && error.response.data || {}]);
});


export default service;